package com.qiyun.dinghuohui.base;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by xlm on 2018\5\25 0025
 */

public class FragmentManager {
    private Context mContext;
    private Fragment mParent;
    private int resId;
    private Fragment mCurrentFragment = null;

    public FragmentManager(Context context, int layoutid) {
        mContext = context;
        resId = layoutid;
    }

    public void showFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        if (mCurrentFragment == fragment) {
            return;
        }
        android.support.v4.app.FragmentManager fm = null;
        fm = ((FragmentActivity) mContext).getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (!fragment.isAdded()) {
            ft.add(resId, fragment);
        }
        if (mCurrentFragment != null)
            ft.hide(mCurrentFragment);
        ft.show(fragment);
        ft.commit();
        mCurrentFragment = fragment;
    }


    public FragmentManager(Fragment fragment, int layoutid) {
        mParent = fragment;
        resId = layoutid;
    }

    public void showChildFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        if (mCurrentFragment == fragment) {
            return;
        }
        android.support.v4.app.FragmentManager fm = null;
        fm = mParent.getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
//      ft.setCustomAnimations(R.anim.in_fragment, R.anim.out_fragment);
        if (!fragment.isAdded()) {
            ft.add(resId, fragment);
        }
        if (mCurrentFragment != null)
            ft.hide(mCurrentFragment);
        ft.show(fragment);
        ft.commit();
        mCurrentFragment = fragment;
    }

    public void hideFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        android.support.v4.app.FragmentManager fm = null;
        fm = ((FragmentActivity) mContext).getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
//      ft.setCustomAnimations(R.anim.in_fragment, R.anim.out_fragment);
        if (!fragment.isAdded()) {
            return;
        }
        ft.hide(fragment);
        ft.commit();
        if (mCurrentFragment == fragment) {
            mCurrentFragment = null;
        }
    }

    public void hideChildFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        android.support.v4.app.FragmentManager fm = null;
        fm = mParent.getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
//      ft.setCustomAnimations(R.anim.in_fragment, R.anim.out_fragment);
        if (!fragment.isAdded()) {
            return;
        }
        ft.hide(fragment);
        ft.commit();
        if (mCurrentFragment == fragment) {
            mCurrentFragment = null;
        }
    }
}
